// pages/xq_blue/xq_search_blue/xq_search_blue.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    blueLists: [],
    xq_test: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("开始监听蓝牙设备搜索")

    var self = this

    wx.onBluetoothDeviceFound(function(res) {
      
      if (res.devices[0].name == "") {
        return
      }

      for (var i = 0; i < self.data.blueLists.length; i++) {
        if (self.data.blueLists[i].deviceId == res.devices[0].deviceId) {
          return
        }
      }

      var newarray = [res.devices[0]]
      self.setData({
        blueLists: self.data.blueLists.concat(newarray)
      })

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.xq_searchBlue()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    console.log("停止蓝牙搜索")
    wx.stopBluetoothDevicesDiscovery({
      success: function(res) {},
    })
  },

  xq_searchBlue: function() {

    wx.getConnectedBluetoothDevices({
      success: function(res) {
        console.log(res)
      },
    })

    wx.startBluetoothDevicesDiscovery({
      success: function(res) {
        console.log("搜索")
        console.log(res)
      },
    })
  },

  xq_stopSearchBlue: function() {
    wx.stopBluetoothDevicesDiscovery({
      success: function(res) {
        console.log("停止搜索")
        console.log(res)
      },
    })
  },

  xq_tapBlueDevice: function(data) {
    console.log("点击了蓝牙设备")
    var item = data.target.dataset.item
    console.log(item)

    wx.showLoading({
      title: '正在连接蓝牙',
    })


    let self = this
    wx.createBLEConnection({
      deviceId: item.deviceId,
      success: function(res) {

        wx.hideLoading()

        if (res.errCode == 0) {
          self.xq_pushFile(item.deviceId)

          // 停止搜索
          wx.stopBluetoothDevicesDiscovery({
            success: function(res) {},
          })

        } else {
          wx.showToast({
            title: '连接失败',
            icon: 'none'
          })
        }

      },

    })
  },


  xq_pushFile: function(deviceId) {
    wx.showActionSheet({
      itemList: ["发送文件", "测试"],
      success: function(res) {
        if (res.tapIndex == 0) {
          wx.navigateTo({
            url: "../xq_send_file/xq_send_file?deviceId=" + deviceId
          })
        } else if (res.tapIndex == 1) {
          wx.navigateTo({
            url: "../xq_receiveFile/xq_receiveFile?deviceId=" + deviceId
          })
        }
      },
      fail: function(res) {
        wx.closeBLEConnection({
          deviceId: deviceId,
          success: function(res) {
            console.log("断开连接")
            console.log(res)
          },
        })
      }
    })
  },


  // 比较版本
  compareVersion: function(v1, v2) {
    v1 = v1.split('.')
    v2 = v2.split('.')
    const len = Math.max(v1.length, v2.length)

    while (v1.length < len) {
      v1.push('0')
    }
    while (v2.length < len) {
      v2.push('0')
    }

    for (let i = 0; i < len; i++) {
      const num1 = parseInt(v1[i])
      const num2 = parseInt(v2[i])

      if (num1 > num2) {
        return 1
      } else if (num1 < num2) {
        return -1
      }
    }

    return 0
  },



















  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }

})